package com.penngo.gui.dfdiff.view;

import com.penngo.gui.dfdiff.DFMain;
import com.penngo.gui.dfdiff.utils.ImgUtil;
import com.penngo.gui.dfdiff.utils.ScreenUtil;

import javax.swing.*;
import javax.swing.event.InternalFrameAdapter;
import javax.swing.event.InternalFrameEvent;
import java.util.List;


public class DesktopFrame extends JInternalFrame {

    private static int openFrameCount = 0;
    private static final int xOffset = 5, yOffset = 5;
    private int width = (int)(ScreenUtil.getScreenWidth() * 0.8) - 300;
    private int height = (int)(ScreenUtil.getScreenHeight() * 0.8) - 150;
//    protected ProjectTable table;
    protected ProjectNode node;

    public DesktopFrame(ProjectNode node) {
//        this.table = DFMain.projectTable;
        this.node = node;
        this.setTitle(node.getTitle());
        //setFrameIcon(ImgUtil.getIcon("/images/logo.png"));
        initFrameIcon();
        addInternalFrameListener(new FrameInternalFrameAdapter());

        setSize(width, height);
        setResizable(true);
        setClosable(true);
        setIconifiable(true);
        setMaximizable(true);
//        pack();

        setLocation(xOffset * openFrameCount++, yOffset * openFrameCount++);

    }

    public void resetLocation(){
        setLocation(xOffset * openFrameCount++, yOffset * openFrameCount++);
    }

    public static void resetOpenFrameCount(){
        openFrameCount = 0;
    }
    private void initFrameIcon(){
        switch (node.getType()){
            case FILE:
                setFrameIcon(ImgUtil.IMAGE_FILE);
                break;
            case FOLDER:
                setFrameIcon(ImgUtil.IMAGE_FOLDER);
                break;
            case FILE_DIFF:
                setFrameIcon(ImgUtil.IMAGE_FILE_DIFF);
                break;
            case FOLDER_DIFF:
                setFrameIcon(ImgUtil.IMAGE_FOLDER_DIFF);
                break;
            default:
                setFrameIcon(ImgUtil.IMAGE_LOGO);
        }
    }
    public void setFrameSelected(boolean selected)  {
        try {
            super.setSelected(true);
        } catch (java.beans.PropertyVetoException e) {
        }
    }
    public ProjectNode getProjectNode(){
        return node;
    }

    class FrameInternalFrameAdapter extends InternalFrameAdapter{
        // 删除节点
        public void internalFrameClosed(InternalFrameEvent e) {
            ProjectNode projectNode = (ProjectNode)DFMain.projectTable.getRootNode();
            List<ProjectNode>  children = projectNode.getChildren();
            for(int index = 0; index < children.size(); index++){
                if(node.getType().equals(ProjectNode.ProjectType.FOLDER)){
                    node.setOpened(false);
                }
                else {
                    if(children.get(index).equals(node)){
                        children.remove(node);
                        DFMain.projectTable.refreshUI();
                        break;
                    }
                }
            }
        }
        public void internalFrameOpened(InternalFrameEvent event) {

        }

        public void internalFrameActivated(InternalFrameEvent event) {
            ProjectTable table = DFMain.projectTable;
            int count = DFMain.projectTable.getRowCount();
            for(int index = 0; index < count; index++){
                ProjectNode nodeTemp = (ProjectNode)table.getValueAt(index, 0);
                if(node.equals(nodeTemp)){
                    table.setRowSelectionInterval(index, index);
                }
            }

        }

    }


    public void undo(){
        //getEditor().getTextArea().undoLastAction();
    }
    public void redo(){
        //getEditor().getTextArea().redoLastAction();
    }
    public void copy(){
        //getEditor().getTextArea().copy();
    }
    public void paste(){
        //getEditor().getTextArea().paste();
    }
    public void cut(){
        //getEditor().getTextArea().cut();
    }
    public void find(){

//        getEditor().getTextArea().fi
    }

}
